package utils

import (
	"demo/config"
	"demo/models"
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"github.com/smartwalle/alipay/v3"
)

func AliPay(payment *models.Payments,amount string, c *gin.Context) string {
	config := config.GetConfig()
	client, err := alipay.New(config.AppId, config.PrivateKey, config.IsProduction)
	if err != nil {
		logrus.Error("failed to init alipay: ", err)
		return ""
	}
	err = client.LoadAliPayPublicKey(config.AliPubKey)
	if err != nil {
		logrus.Error("failed to load alipay public cert: ", err)
		return ""
	}
	var p = alipay.TradePagePay{}
	p.NotifyURL = fmt.Sprintf("http://43.143.232.128:31005/v1/ok_pay/send/%d", payment.Id) 
	p.ReturnURL = "http://43.143.232.128:7890/charge"
	p.Subject = "测试"
	ts := time.Now().UnixMilli()
	p.OutTradeNo = fmt.Sprintf("%d", ts)
	p.TotalAmount = amount
	p.ProductCode = config.ProductCode

	url, err := client.TradePagePay(p)
	payUrl := url.String()
	return payUrl
	// fmt.Println(payUrl)
	// c.JSON(http.StatusOK,gin.H{
	// 	"msg":"ok",
	// 	"url":payUrl,
	// })
}
